<?php

declare(strict_types=1);

namespace app\common\service\hrm;

use app\common\traits\ServiceTrait;
use app\common\dao\hrm\RankingDao;
use app\common\util\TreeUtil;
use app\common\traits\TreeServiceTrait;

/**
 * 职位 -- 业务
 * @author:kaige
 * @update:2022-06-24
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class RankingService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new RankingDao();
  }
  use ServiceTrait, TreeServiceTrait;

  /**
   * 重写with
   */
  public function _list_with()
  {
    return ['glcompany'];
  }
  /**
   * 重写 表格过滤条件
   * @param $post
   * @return array
   */
  public function _list_where($post)
  {
    $where = [];
    if (isset($post['company_id']) && !empty($post['company_id'])) {
      $where[] = ['company_id', '=', $post['company_id']];
    }
    if (isset($post['status']) && $post['status'] != "") {
      $where[] = ['status', '=', $post['status']];
    }
    if (isset($post['keywords']) && !empty($post['keywords'])) {
      $where[] = ['name|code', 'like', '%' . $post['keywords'] . '%'];
    }
    $result['where'] = $where;
    $result['field'] = ["*,pid as parentId"];
    $result['order'] = ['sort' => 'asc', 'id' => 'desc'];
    return $result;
  }


  /**
   * 重写 根据查询结果，格式化数据
   * @param $list
   * @return mixed
   */
  public function _list_format($list)
  {
    foreach ($list as $k => $v) {
      if (isset($v['pid'])) {
        $list[$k]['pobj'] = $this->getPid($v['pid']);
      }
      if (isset($v['glcompany'])) {
        unset($v['glcompany']);
      }
    }
    return $list;
  }

  /**
   * 根据Id获取部门名称
   */
  public static function getNameById($id)
  {
    return (new RankingDao())->getNameById($id);
  }

  /**
   * 获取部门列表
   * @return array
   */
  public static function getRankingList()
  {
    return (new RankingDao())->getListAll(['status' => 1], ['sort' => 'asc']);
  }

  /**
   * 获取所有列表树型
   * @return array
   */
  public function getRankingTree()
  {
    $data = $this->dao->getListAll(true, ['sort' => 'asc']);
    return $this->createTree($data, 0);
  }

  /**
   * 获取树型
   */
  public function rankingTree()
  {
    $tree = [];
    $list = $this->dao->getListAll(['status' => 1], ['sort' => 'asc'], ['name,id as value,id,pid,0 as user']);
    if ($list) {
      $tree = TreeUtil::list_to_tree($list, 0, 'id', 'pid', 'children');
    }
    return $tree;
  }

  /**
   * 把数组构建成一维的有缩进的树
   * @param $list
   * @param int $pid
   * @return array
   */
  public function createTree($list, $pid = 0, $level = 1)
  {
    $data = [];
    $str = "";
    for ($i = 0; $i < $level; $i++) {
      if ($i > 0) $str .= ' ';
    }
    if ($level == 1) {
      $str .= '';
    } else {
      $str .= ' ';
    }
    foreach ($list as $k => $v) {
      if ($v["pid"] == $pid) {
        $v['name'] = $str . $v['name'];
        $data[] = $v;
        $ch = $this->createTree($list, $v['id'], $level + 1);
        $data = array_merge($data, $ch);
      }
    }
    return $data;
  }

  /**
   * 获取所有部门数组-(id,pid,name,sort)
   * @return array
   */
  public function getRankingArray()
  {
    $data = $this->dao->getListAll(true, ['sort' => 'asc'], ['id,pid,name,sort']);
    return $data;
  }

  /**
   * 获取部门数组，加入all元素
   * @return array
   */
  public function getRankingArray2()
  {
    $all = ['id' => 'all', 'name' => '全部', 'pid' => 0, 'sort' => 0];
    $data  = $this->getRankingArray();
    array_unshift($data, $all);
    return $data;
  }

  /**
   * 获取父部门
   * @param pid
   * @return object
   */
  public function getPid($pid)
  {
    $parentinfo = $this->dao->getInfoById($pid);
    return $parentinfo;
  }

  /**
   * 递归取得所有的父节点
   */
  public function getPids($id)
  {
    $data = [];
    $info = $this->dao->getInfoById($id);
    if (!$info) {
      return $data;
    }
    $info = object_array($info);
    //判断是否还有父节点，如果有，就取父节点，如果没有，就返回了
    if ($info['pid'] != 0) {
      $data = $this->getPids($info['pid']); //返回空数组或者二维数组
    }
    array_push($data, $info);
    return $data;
  }
}
